package com.peak.distribution.jms;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import com.peak.spring.boot.core.mapper.JsonMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class JmsProducer {

	@Autowired
	private JmsMessagingTemplate jmsTemplate;

	/**
	 * 生产一个Queue的消息
	 * @param queueName 消息的名称，消费者需要绑定才行的
	 * @param message 可以传很多对象，会直接转成JSON的String
	 */
	public void sendQueue(String queueName, Object message) {
		log.info("发布消息【{}】: {}", queueName, message);
		Destination destination = new ActiveMQQueue(queueName);
		String json = JsonMapper.nonDefaultMapper().toJson(message);
		jmsTemplate.convertAndSend(destination, json);
	}
}
